package com.smmisia.auth.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.smmisia.common.entity.auth.SysRole;
import com.smmisia.common.entity.auth.SysRoleMenu;
import com.smmisia.common.entity.auth.SysUserRole;
import com.smmisia.common.entity.auth.User;
import com.smmisia.common.entity.auth.convertor.SysRoleConvertor;
import com.smmisia.common.enums.DeleteEnum;
import com.smmisia.common.enums.StatusEnum;
import com.smmisia.common.request.auth.RoleRequest;
import com.smmisia.auth.mapper.SysRoleMapper;
import com.smmisia.auth.mapper.SysRoleMenuMapper;
import com.smmisia.auth.mapper.SysUserRoleMapper;
import com.smmisia.auth.mapper.UserMapper;
import com.smmisia.auth.service.SysRoleService;
import com.smmisia.common.Result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 角色服务实现类
 *
 * @author smmisia
 * @date 2024/7/27
 */
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole>
        implements SysRoleService {

    @Autowired
    private SysRoleMapper sysRoleMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private SysUserRoleMapper sysUserRoleMapper;

    @Autowired
    private SysRoleMenuMapper sysRoleMenuMapper;

    @Override
    public Result<String> addRole(RoleRequest roleRequest) {
        SysRole role = SysRoleConvertor.INSTANCE.toEntity(roleRequest);
        LambdaQueryWrapper<SysRole> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(SysRole::getRoleKey, role.getRoleKey());
        SysRole sysRole = sysRoleMapper.selectOne(wrapper);
        if (sysRole != null) {
            return Result.fail("权限名已存在");
        }
        LambdaQueryWrapper<SysRole> wrappers = Wrappers.lambdaQuery();
        wrappers.eq(SysRole::getRoleName, role.getRoleName());
        SysRole sysRoles = sysRoleMapper.selectOne(wrapper);
        if (sysRoles != null) {
            return Result.fail("角色名已存在");
        }
        Integer loginId = (Integer) StpUtil.getLoginId();
        User user = userMapper.selectById(loginId);
        role.setStatus(String.valueOf(StatusEnum.NORMAL));
        role.setDelFlag(DeleteEnum.UN_DELETE.getDesc());
        role.setCreateBy(user.getUsername());
        role.setCreateTime(LocalDateTime.now());
        int result = sysRoleMapper.insert(role);
        Assert.isTrue(result > 0, () -> new RuntimeException("添加失败"));
        return Result.ok("添加成功");
    }

    @Override
    public Result<String> deleteRole(Integer id) {
        LambdaQueryWrapper<SysUserRole> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(SysUserRole::getRoleId, id);
        if (sysUserRoleMapper.selectCount(wrapper) > 0) {
            return Result.fail("该角色下存在用户，无法删除");
        }
        LambdaQueryWrapper<SysRoleMenu> wrappers = Wrappers.lambdaQuery();
        wrappers.eq(SysRoleMenu::getRoleId, id);
        if (sysRoleMenuMapper.selectCount(wrappers) > 0) {
            return Result.fail("该角色下存在菜单，无法删除");
        }
        int result = sysRoleMapper.deleteById(id);
        Assert.isTrue(result > 0, () -> new RuntimeException("删除失败"));
        return Result.ok("删除成功");
    }

    @Override
    public Result<String> updateRole(RoleRequest roleRequest) {
        Integer loginId = (Integer) StpUtil.getLoginId();
        User user = userMapper.selectById(loginId);
        SysRole sysRole = sysRoleMapper.selectById(roleRequest.getRoleId());
        LambdaQueryWrapper<SysRole> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(SysRole::getRoleName, roleRequest.getRoleName());
        SysRole role = sysRoleMapper.selectOne(wrapper);
        if (role != null) {
            return Result.fail("角色名不能重复");
        }
        sysRole.setRoleName(roleRequest.getRoleName());
        sysRole.setRoleKey(roleRequest.getRoleKey());
        sysRole.setRoleSort(roleRequest.getRoleSort());
        sysRole.setUpdateBy(user.getUsername());
        sysRole.setUpdateTime(LocalDateTime.now());
        int result = sysRoleMapper.updateById(sysRole);
        Assert.isTrue(result > 0, () -> new RuntimeException("修改失败"));
        return Result.ok("修改成功");
    }

    @Override
    public Result<List<SysRole>> getRole(Integer pageNum, Integer pageSize) {
        IPage<SysRole> page = new Page<SysRole>(pageNum, pageSize);
        List<SysRole> roleList = sysRoleMapper.selectPage(page, null).getRecords();
        return Result.ok(roleList);
    }

    @Override
    public Result<SysRole> getRoleInfo(Integer id) {
        SysRole role = sysRoleMapper.selectById(id);
        return Result.ok(role);
    }

}




